package epoch.game.core.config;

import epoch.game.core.exception.SysException;
import epoch.game.core.util.Prop;

/**
 * 服务器配置
 * 
 * @author will
 * @date 2015年6月10日 下午2:55:54
 */
public class ServerConfig {
	
	//名称
	private String Name;
	//服务器id
	private int serverId;
//	//服务器显示id
//	private int serverShowId;
	//平台名称
	private String agentName;
	//IP地址
	private String IPAddress;
	//url地址
	private String url;
	//外网端口
	private int port;
	//ssl端口
	private int sslPort;
	//nio线程数
	private int ioThreadSize;
	
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public int getServerId() {
		return serverId;
	}
	public void setServerId(int serverId) {
		this.serverId = serverId;
	}
//	public void setServerShowId(int id) {
//		this.serverShowId = id;
//	}
//	public int getServerShowId() {
//		return this.serverShowId;
//	}
	public String getAgentName() {
		return agentName;
	}
	public void setAgentName(String agentName) {
		this.agentName = agentName;
	}
	public String getIPAddress() {
		return IPAddress;
	}
	public void setIPAddress(String iPAddress) {
		IPAddress = iPAddress;
	}	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public int getSslPort() {
		return sslPort;
	}
	public void setSslPort(int sslPort) {
		this.sslPort = sslPort;
	}
	
	public int getIoThreadSize() {
		return ioThreadSize;
	}
	public void setIoThreadSize(int ioThreadSize) {
		this.ioThreadSize = ioThreadSize;
	}
	
	public static ServerConfig load(String configPath) {
		try {
			Prop prop = Prop.load(configPath);
			return load(prop);
		} catch (Exception ex) {
			throw new SysException("加载服务器配置出错，confin=ServerConfig,configPath=" + configPath, ex);
		}
	}
	public static int getShowIdByServerId(int serverId) {
		int showId = serverId%10000;
		if (showId == 0) showId = serverId;
		return showId;
	}
	public static ServerConfig load(Prop prop) {
		ServerConfig config = new ServerConfig();
		config.setName(prop.get("serverName").trim());
		int serverId = Integer.valueOf(prop.get("serverId").trim());
		config.setServerId(serverId);
//		config.setServerShowId(getShowIdByServerId(serverId));
		config.setAgentName(prop.get("agent").trim());
		if (prop.containsKey("ip")) {
			config.setIPAddress(prop.get("ip").trim());
		}
		if (prop.containsKey("url")) {
			config.setUrl(prop.get("url").trim());
		}
		if (prop.containsKey("port")) {
			config.setPort(prop.getInt("port"));
		}
		if (prop.containsKey("sslport")) {
			config.setSslPort(prop.getInt("sslport"));
		}
		config.ioThreadSize=prop.getInt("ioThreadSize", Runtime.getRuntime().availableProcessors()/2+1);
		return config;
	}
}
